package com.yaowen.myapplication;

import android.annotation.TargetApi;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LeftFragment.MyListener {

    /**
     * 实现MyListener,当LeftFragment中点击第一页的时候，让RightFragment显示第一页信息,同理当点击第二页的时候，RightFragment显示第二页信息
     *
     * @param index 显示的页数
     */
    public void showMessage(int index) {
        if (1 == index) {
            showMessageView.setText(R.string.first_page);
        }
        if (2 == index) {
            showMessageView.setText(R.string.second_page);
        }
        if (3 == index) {
            showMessageView.setText(R.string.third_page);
        }
    }
    /** 得到RightFragment中显示信息的控件 */
    private TextView showMessageView;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("MainActivity", "onCreate");
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction transaction= fragmentManager.beginTransaction();
        //动态添加fragment
        RightFragment rightFragment=new RightFragment();
        LeftFragment leftFragment=new LeftFragment();
        transaction.add(R.id.linearLayout_left,leftFragment,"leftFragment");
        transaction.add(R.id.linearLayout_right,rightFragment,"rightFragment");
        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MainActivity", "onResume");
        //注意：findview放到这里
        showMessageView= (TextView) findViewById(R.id.rigth_show_message);

    }
}
